﻿using AutoMapper;
using MediatR;
using SmartCity.ProcessEngine.API.Write.Application.Commands.Forms;
using SmartCity.ProcessEngine.Domain;
using SmartCity.ProcessEngine.Errorcode;
using SmartCity.ProcessEngine.Infrastructure.Interfaces;

namespace SmartCity.ProcessEngine.API.Write.Application.CommandHandlers.Forms
{
    /// <summary>
    /// 处理
    /// </summary>
    public class FormDeleteCommandHandler:IRequestHandler<FormDeleteCommand,ApiResult<int>>
    {
        private readonly IBaseRepository<WF_Form> formRep;
        private readonly IMapper mapper;

        public FormDeleteCommandHandler(IBaseRepository<WF_Form> formRep,IMapper mapper)
        {
            this.formRep = formRep;
            this.mapper = mapper;
        }

        /// <summary>
        /// 删除表单
        /// </summary>
        /// <param name="request">请求</param>
        /// <param name="cancellationToken">取消</param>
        /// <returns>返回任务</returns>
        public async Task<ApiResult<int>> Handle(FormDeleteCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> result = new ApiResult<int>();
            result.code = ApiEnum.DeleteSuccess;
            result.message = "删除成功";

            try
            {
                WF_Form entity = mapper.Map<WF_Form>(request);
                entity.IsDeleted = true;
                await formRep.UpdateAsync(entity);
            }
            catch (Exception ex)
            {
                result.code = ApiEnum.DeleteFail;
                result.message = ex.Message;
            }
            return await Task.FromResult(result);
        }
    }
}
